package com.xnyzc.lhy.order.service.impl.order;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.penum.EOrderType;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.order.entity.param.order.OrderParam;
import com.xnyzc.lhy.order.entity.order.OaCOrderPersonalOfficial;
import com.xnyzc.lhy.order.entity.order.OaDOrder;
import com.xnyzc.lhy.order.entity.gps.OaDVehicleGps;
import com.xnyzc.lhy.order.mapper.order.OaCOrderPersonalOfficialMapper;
import com.xnyzc.lhy.order.service.order.IOaCOrderPersonalOfficialService;
import com.xnyzc.lhy.order.service.order.IOaDOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 个人公务订单 服务实现类
 * </p>
 *
 * @author lhy@xnyzc.com
 * @since 2019-07-22
 */
@Service
public class OaCOrderPersonalOfficialServiceImpl extends ServiceImpl<OaCOrderPersonalOfficialMapper, OaCOrderPersonalOfficial> implements IOaCOrderPersonalOfficialService {

    @Autowired
    private IOaDOrderService oaDOrderService;


    @Override
    public boolean distributeOrderPersonOfficial(OaCOrderPersonalOfficial personalOfficial, OrderParam oaDOrder, OaDVehicleGps vehicleGps) {
        try {
            if (oaDOrderService.addOrder(vehicleGps, oaDOrder)) {
                personalOfficial.setVehicleType(vehicleGps.getVehType());
                personalOfficial.setDriverId(vehicleGps.getDriverId());
                return this.updateById(personalOfficial);
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public String orderPersonOfficial(OaCOrderPersonalOfficial personalOfficial, OrderParam oaDOrder) {
        long id = IDUtil.nextId();
        //生成自费订单
        personalOfficial.setIsCarpool(oaDOrder.getIsCarpool());
        personalOfficial.setCarpoolNumber(oaDOrder.getCarpoolNumber());
        personalOfficial.setOrderNo(oaDOrder.getOrderNo());
        personalOfficial.setPersonalOfficialId(id);
        //是拼单产生流水号
        personalOfficial.setCarpoolSerialNo(oaDOrder.getCarpoolSerialNo());
        if (!this.save(personalOfficial)) {
            throw PangException.create(EErrorCode.noData);
        }
        return String.valueOf(id);
    }
}
